package com.bdsoft.actions.UserManage.Service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.bdsoft.actions.RoleManage.Dao.RoleMapper;
import com.bdsoft.actions.UserManage.DTO.UserDTO;
import com.bdsoft.actions.UserManage.Dao.UserMapper;
import com.bdsoft.actions.UserManage.Service.UserService;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;


    @Override
    public PageInfo<Map<String,Object>> getUserTree(int pageNum, int pageSize, String id, String userName,String status,String nickName) {

        PageHelper.startPage(pageNum, pageSize);
        List<Map<String,Object>> resList=userMapper.selectUserByParams(id,userName,status,nickName);

        return new PageInfo<>(resList);
    }

    @Override
    public Map<String, Object> saveOrUpdateUser(UserDTO userDTO) {

        if (StrUtil.isBlank(userDTO.getUserId())){
            if(StrUtil.isBlank(userDTO.getNickname())) {
                // 默认昵称规则：用户名+随机后缀
                userDTO.setNickname(userDTO.getUsername() + "_" + RandomUtil.randomString(4));
            }
            UUID uuid=UUID.randomUUID();
            UUID uuid1=UUID.randomUUID();
            userDTO.setUserId(uuid.toString());
            userMapper.InsertUserRole(uuid1.toString(),uuid.toString(),"r200");

            int result=userMapper.addUser(userDTO);
            return result>0?MapUtil.<String,Object>builder()
                    .put("code",200)
                    .put("data","添加用户成功")
                    .build():
                    MapUtil.<String,Object>builder()
                            .put("code",400)
                            .put("data","添加用户失败")
                            .build();
        }else{
            if(StrUtil.isBlank(userDTO.getNickname())) {
                UserDTO existingUser = userMapper.selectById(userDTO.getUserId());
                if(existingUser != null) {
                    userDTO.setNickname(existingUser.getNickname());
                }
            }
            int result=userMapper.updateUserById(userDTO);
            return result>0?MapUtil.<String,Object>builder()
                    .put("code",200)
                    .put("data","修改用户成功")
                    .build():
                    MapUtil.<String,Object>builder()
                            .put("code",400)
                            .put("data","修改用户失败")
                            .build();
        }


    }

    @Override
    public Map<String, Object> deleteUserById(String userId) {
        int result = userMapper.deleteUserById(userId);
        return result > 0 ? MapUtil.<String,Object>builder()
                .put("code", 200)
                .put("data", "删除用户成功")
                .build()
            : MapUtil.<String,Object>builder()
                .put("code", 400)
                .put("data", "删除用户失败(或者找不到用户)")
                .build();
    }

    @Override
    public Map<String, Object> allocateRole(String userId, String roleIds) {


        List<String> roleIdList=StrUtil.split(roleIds,",");


        userMapper.deleteUserRole(userId);
        for (String s:roleIdList){
            UUID uuid=UUID.randomUUID();
            userMapper.InsertUserRole(uuid.toString(),userId,s);
        }


        return  MapUtil.<String,Object>builder()
                .put("code", 200)
                .put("data", "分配角色成功")
                .build();
    }

    @Override
    public Map<String, Object> getRoleByRole(String userId) {
        return MapUtil.<String,Object>builder()
                .put("code", 200)
                .put("data", roleMapper.selectRoleByUserId(userId))
                .build();
    }

    @Override
    public Map<String, Object> setDepartment(String userId, String departMentId) {
        return MapUtil.<String,Object>builder()
                .put("code", 200)
                .put("data", userMapper.setDepartment(userId,departMentId))
                .build();
    }
}
